***Merging SIPP core files and select topical modules across panels***
*Author - Max Kellogg (maxkllgg@gmail.com)

*Use SIPP

*Waves per panel:
local wcount08 16
local wcount04 12
local wcount01 9
local wcount96 12

*filepath across panels:
local doname08 sippl08puw
local doname04 sippl04puw
local doname01 sip01w
local doname96 sip96l

local wname08 sippl08puw
local wname04 sippl04puw
local wname01 sipp01w
local wname96 sipp96l

*filepath architecture for topical modules:
local tdoname08 sippp08putm
local tdoname04 sippp04putm
local tdoname01 sip01t
local tdoname96 sip96t

local tname08 sippp08putm
local tname04 sippp04putm
local tname01 sipp01t
local tname96 sipp96t

*Topical modules I will pull from for each panels
*tops_main contains employment history
*tops_dis contains subjective work  limitations
*tops_other is... everything else
local tops_main08 1
local tops_other08 4 6 7 10
local tops_dis08 2 
local tops_main04 1
local tops_other04 3 5 6
local tops_dis04 2 
local tops_main01 1
local tops_other01 3 5 6 8 9
local tops_dis01 2 
local tops_main96 1
local tops_other96 3 5 6 9 11 12
local tops_dis96 2 

foreach wave in 08 04 01 96 {
***Commenting out the below loop. It only needs to be run once*****
***It loops over do-files provided by NBER, which imports and cleans DAT files***
/*
capture log close
forvalues i=1(1)`wcount`wave''{
clear all
do "../raw/core/`doname`wave''`i'.do"
log close
if("`wave'" == "96") save "../raw/core/`wname`wave''`i'.dta", replace
}

foreach i in `tops_other`wave'' `tops_dis`wave'' { 
clear all
do "../raw/topical/`tdoname`wave''`i'.do"
capture log close
if("`wave'" == "96") save "../raw/topical/`tname`wave''`i'.dta", replace
}
*/

use "./raw/core/`wname`wave''1.dta", clear

forvalues i=2(1)`wcount`wave''{
append using "./raw/core/`wname`wave''`i'.dta"
}


duplicates report spanel swave ssuid srefmon epppnum

merge m:1 ssuid spanel epppnum using "./raw/topical/`tname`wave''`tops_dis`wave''.dta", keepusing(*lmtmo *prev*mo *lmtyr *prev*yr)
drop _merge

capture rename eprevmo eprevbmo
capture rename tprevyr tprevbyr
capture rename aprevmo aprevbmo
capture rename aprevyr aprevbyr

foreach j in `tops_main`wave'' {
merge m:1 ssuid spanel swave epppnum using "./raw/topical/`tname`wave''`j'.dta", keepusing(*frmryr *lstwrky *makmnyr *ssistry *aplssi) replace update
drop _merge
}


foreach j in `tops_other`wave'' {
merge m:1 ssuid spanel swave epppnum using "./raw/topical/`tname`wave''`j'.dta", replace update
drop _merge

}


sort ssuid spanel epppnum swave srefmon
keep if eoutcome == 201 |eoutcome == 203 | eoutcome == 207

*edisabl, edisprev, elmtver, eprevwk, tlmtmo, elmtyr, tprevbmo, tprevbyr




global vars tpearn sp_tpearn tempDIstate
*ssagess sssmnyn ssi_mnyn ssi_emonth ssi_bmonth
foreach var in sex popstat bmnth ms disabl applyss disprev ptwrk hrsall pdjbthn jbhrs1 jbhrs2 lmtmo prevbmo ssself ssiself tenure mrtown ast3e main1 cond1 cond2 cond3 educ cane wchair hearaid seedif heardif hearnot speechd dif10 dif25 pushd standd sitd stoopd reachd graspd stairsd walkd teled indif outdif beddif bathdif dressd walk2d eatdif toiletd moneyd mealsd hworkd medd race aplssi pdjbthn biznow1 biznow2 { 
capture noisily replace e`var' = . if a`var' != 0 	& a`var' != 3
global vars $vars e`var'
if (_rc == 0 & "`var'" == "hrsall") local hrsvar hrsall
if (_rc == 0 & "`var'" == "jbhrs1" | "`var'" == "jbhrs2") local hrsvar ejbhrs1 ejbhrs2
}


foreach var in byear lmtyr prevbyr agess 01amta hhtnw hhbeq frmryr lstwrky makmnyr ssistry  {
capture noisily replace t`var' = . if a`var' != 0	& a`var' != 3
global vars $vars t`var'
}

replace essself = . if essself == -1
replace essself = 0 if essself == 2
replace essiself = . if essiself == -1
replace essiself = 0 if essiself == 2
replace eapplyss = . if eapplyss == -1
replace eapplyss = 0 if eapplyss == 2

*No imputation?
* pearn hearn fearn sfearn
bysort spanel ssuid epppnum: egen firstyear = min(rhcalyr + (rhcalmn-1)/12 + 9999*(edisabl == -1 | missing(edisabl)))
replace firstyear = . if firstyear >= 9999

bysort spanel ssuid epppnum: egen firstyearprev = min(rhcalyr + (rhcalmn-1)/12 + 9999*(edisabl == -1 | missing(edisabl) | edisprev == -1 | missing(edisprev)))
replace firstyearprev = . if firstyearprev >= 9999

gen year_limit = .
replace year_limit = tlmtyr + (elmtmo-1)/12 if !missing(tlmtyr) & tlmtyr >=0  & !missing(elmtmo) & elmtmo >= 0 
replace year_limit = . if year_limit > firstyear /// if so, disability should be observed in the monthly survey data.

bysort spanel ssuid epppnum: egen year_limit2 = min(rhcalyr + (rhcalmn-1)/12 + 9999*(edisabl != 1)) 
replace year_limit2 = . if year_limit2 >= 9999
replace year_limit2 = . if year_limit2 == firstyear 

replace year_limit = year_limit2 if (missing(year_limit) & !missing(year_limit2)) | (year_limit2 < year_limit)
drop year_limit2

gen year_prev= .
replace year_prev = tprevbyr + (eprevbmo-1)/12 if !missing(tprevbyr) & tprevbyr >=0  & !missing(eprevbmo) & eprevbmo >= 0
replace year_prev = . if year_prev > firstyearprev /// if so, disability should be observed in the monthly survey data.

bysort spanel ssuid epppnum: egen year_prev2 = min(rhcalyr + (rhcalmn-1)/12  + 9999*(edisprev != 1)) 
replace year_prev2  = . if year_prev2 >= 9999
replace year_prev2 = . if year_prev2 == firstyearprev

replace year_prev = year_prev2 if (missing(year_prev) & !missing(year_prev2)) | (year_prev2 < year_prev)
drop year_prev2

*For some reason, people can claim to have a preventing condition before a limiting condition:
replace year_limit = year_prev if year_prev < year_limit


gen time_limit = rhcalyr + (rhcalmn-1)/12 - year_limit
gen time_prev = rhcalyr + (rhcalmn-1)/12 - year_prev




replace time_limit = round(time_limit,0.01)
replace time_prev = round(time_prev,0.01)
replace time_prev = . if year_limit > year_prev

 destring epppnum, replace

preserve
tempfile spouses
ren epppnum sp_epppnum
ren epnspous epppnum
drop if epppnum == 9999

duplicates tag spanel swave ssuid srefmon epppnum, gen(tag)
drop if tag != 0
drop tag

keep spanel tage swave ssuid srefmon epppnum sp_epppnum tpearn edisabl eapplyss edisprev year_limit year_prev `hrsvar' essself essiself eeduc esex ems

foreach x in tage tpearn edisabl edisprev year_limit year_prev `hrsvar' essself essiself eeduc esex ems {
ren `x' sp_`x'
}
save `spouses', replace
restore
merge 1:1 spanel swave ssuid srefmon epppnum using `spouses'
*_merge == 2 means you are a spouse, but for a person who isn't in the data.
drop if _merge == 2 
drop _merge

 
 *States with temporary DI programs, comparable to SSDI
* CA, HI, RI, NJ, NY
gen tempDIstate =  tfipsst == 6 |  tfipsst == 44 | tfipsst  == 34
gen tempDIstate_low =  tempDIstate == 1 | tfipsst == 36
*bysort spanel swave ssuid epppnum: egen tempDIstate2 = max(tempDIstate)
*drop tempDIstate
*rename tempDIstate2 tempDIstate
*NY has one, but with very low payments.
*HI also has one

keep if tage >= 23 & tage <= 62


save "./combined/sipp_combined`wave'", replace
}

use "./combined/sipp_combined08", clear
*keep if year_limit != .
foreach wave in 04 01 96 {
append using "./combined/sipp_combined`wave'.dta"
*keep if year_limit != .
}
keep epppnum ssuid spanel swave rhcalmn rhcalyr srefmon tage $vars econd1 econd2 econd3 tempDIstate tfipsst time_* year* firstyear*  sp_* wpfinwgt east3e

tokenize 150500	150900	151200	151800	152100	152400	152600	152900	153100	153500	153700	153900	154700	155000	155500	156100	156400	156700	157000	157200	157700	158200	158700	159100	159400	159700	159800	159900	159900	160200	160400	160800	161200	161500	161700	161800	162000	162000	162000	162200	162600	162800	163200	163400	163500	163900	164100	164400	164700	164700	164800	165900	166000	166000	166700	167100	167800	168100	168400	168800	169300	170000	171000	170900	171200	172200	172700	172700	173600	173900	174200	174600	175600	176000	176100	176400	177300	177700	177400	177400	178100	177600	177500	177400	177700	178000	178500	179300	179500	179600	180000	180500	180800	181200	181500	181800	182600	183600	183900	183200	182900	183100	183700	184500	185100	184900	185000	185500	186300	186700	187100	187400	188200	188900	189100	189200	189800	190800	191700	191700	191600	192400	193100	193700	193600	193700	194900	196100	198800	199100	198100	198100	199300	199400	199700	200700	201300	201800	202900	203800	202800	201900	202000	203100	203437	204226	205288	205904	206755	207234	207603	207667	208547	209190	210834	211445	212174	212687	213448	213942	215208	217463	219016	218690	218877	216995	213153	211398	211933	212705	212495	212709	213022	214790	214726	215445	215861	216509	217234	217347	217488	217281	217353	217403	217290	217199	217605	217923	218275	219035	219590	220472	221187	221898	223046	224093	224806	224806	225395	226106	226597	226750	227169	227223	227842	228329	228807	229187	228713	228524	228590	229918	231015	231638	231249	231221	231679	232937	232282	231797	231893	232445	232900	233456	233544	233669	234100	234719

gen price = .
forvalues y = 1995(1)2013{
	forvalues m = 1(1)12{
		*normalizing by 1996 avg CPI
		replace price = `1'/156900 if rhcalyr == `y' & rhcalmn == `m' 
		macro shift
	}
}

foreach x in tpearn sp_tpearn {
gen `x'_real = `x' / price
}

save "./sipp_combined", replace



